package com.design.pattern.factory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author 王霄
 * @date 2022/3/18 22:02
 */
public class CarFactory {

    private static final Map<CarType, Car> CAR_MAP = new HashMap<>();

    public CarFactory(List<Car> carList) {
        carList.forEach(car -> CAR_MAP.putIfAbsent(car.getCarType(), car));
    }

    public Car get(CarType carType) {
        return Optional.ofNullable(CAR_MAP.get(carType))
                .orElseThrow(() -> new RuntimeException("未找到对应的汽车工厂"));
    }
}
